package com.weic.controller;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.server.ZooKeeperServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

@RequestMapping(value = "/zk")
@Controller
public class ZookeeperController {

    @RequestMapping(value = "/helloZookeeper.json")
    public @ResponseBody String helloZookeeper() throws Exception {
        //Watch:观察回调
        //new 的时候连接并没有马上建立
        //如何保证new之后zooKeeper连接已建立：使用CountDownLatch
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper("192.168.10.61:2181,192.168.10.62:2181,192.168.10.63:2181,192.168.10.64:2181", 3000, new Watcher() {
            public void process(WatchedEvent event) {
                String path = event.getPath();
                Event.KeeperState state = event.getState();
                Event.EventType type = event.getType();
                System.out.println(event.toString());
                switch (type) {
                    case None:
                        break;
                    case NodeCreated:
                        break;
                    case NodeDeleted:
                        break;
                    case NodeDataChanged:
                        break;
                    case NodeChildrenChanged:
                        break;
                }

                switch (state) {
                    case Unknown:
                        break;
                    case Disconnected:
                        break;
                    case NoSyncConnected:
                        break;
                    case SyncConnected:
                        countDownLatch.countDown();
                        System.out.println("connected!");
                        break;
                    case AuthFailed:
                        break;
                    case ConnectedReadOnly:
                        break;
                    case SaslAuthenticated:
                        break;
                    case Expired:
                        break;
                }
            }
        });
        countDownLatch.await();
        ZooKeeper.States state = zooKeeper.getState();
        switch (state) {
            case CONNECTING:
                System.out.println("ing...");
                break;
            case ASSOCIATING:
                break;
            case CONNECTED:
                System.out.println("ed...");
                break;
            case CONNECTEDREADONLY:
                break;
            case CLOSED:
                break;
            case AUTH_FAILED:
                break;
            case NOT_CONNECTED:
                break;
        }
        return "helloZookeeper";
    }
}
